package san.dao.article;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import san.dao.HibernateDao;
import san.entity.article.Article;
import com.utils.Page;

@Repository
public class ArticleDao extends HibernateDao<Article,String>{
	/*
	 * 文章类型列表 获得所有类型文章及文章总数
	 */
	@SuppressWarnings("unchecked")
	public Page getAllArticleAndCount(Page page) {
		Assert.notNull(page, "page不能为空");
		String hql = "select o.id,o.articletype,count(items) from Article o left outer join o.articleItems as items group by o.id,o.articletype";
		Query q = createQuery(hql);
		if (page.isAutoCount()) {
			long totalCount = countHqlResult(hql);
			page.setTotalCount(totalCount);
		}
		setPageParameter(q, page);
		List result = q.list();
		page.setResult(result);
		page.setTotalCount(getCountForAllArticle());
		return page;
	}
	/*
	 * 文章类型列表 获得所有类型文章总数
	 */
	public long getCountForAllArticle() {
		String hql = "select * from Article ";
		return countHqlResult(hql);
	}
	
}
